<?php
namespace app\agent\controller;

//use app\common\model\Shops;
use app\agent\model\Shops;
use app\common\model\Goods;
use app\common\model\Orders;
use think\Controller;
use think\Db;

class Index extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->assign('menu',getMenuArr());
        $this->assign('navigation','admins_index');
        $this->assign('title','管理员列表');
        return $this->fetch();
    }

    public function user(){
        return $this->fetch();
    }

    public function welcome(){
//        dump($this->agent_manage_id);die;
        $user = new Shops();
        //今日商户增加数
        $nowcount = $user->where('sp_agem_id',$this->agent_manage_id)->where("date_format(now(),'%Y%m%d') = from_unixtime(sp_create_time,'%Y%m%d')")->field("count(sp_id) sum")->find();
//        dump($nowcount);die;
        //昨日商户增加数
        $yesterday = $user->where('sp_agem_id',$this->agent_manage_id)->where(" DATE_SUB(CURDATE(), INTERVAL 1 DAY) = from_unixtime(sp_create_time,'%Y%m%d')")->field("count(sp_id) sum")->find();
        //本月商户增加数
        $month = $user->where('sp_agem_id',$this->agent_manage_id)->where("date_format(now(),'%Y%m') = from_unixtime(sp_create_time,'%Y%m')")->field("count(sp_id) sum")->find();
        //所有商户
        $userall = $user->where('sp_agem_id',$this->agent_manage_id)->count();
        $shop = Shops::where('sp_agem_id',$this->agent_manage_id)->count();//商户数
        $goods = Db::name('shops')
            ->join('goods','gd_shop_id=sp_id')
            ->join('agent_manage','sp_agem_id=agem_id')->where('agem_id',$this->agent_manage_id)->count();//产品数

        $order = Orders::where('ord_status',1)->count();
        $this->assign('order',$order);
        $this->assign('shop',$shop);
        $this->assign('goods',$goods);
        $this->assign('nowcount',$nowcount);
        $this->assign('yesterday',$yesterday);
        $this->assign('month',$month);
        $this->assign('userall',$userall);
        return $this->fetch();
    }
    public function ww(){
        $res = [5,20,36,10,10,20];
        echo json_encode($res);
    }

    /**
     * 首页图标元数据
     * @return array
     */
    public function report(){
        $type = $this->request->get('type');
        $date = $this->request->get('date');
        if($type=='order'){
            return $this->order($date);
        }
        if($type == 'userinfo'){
            return $this->userinfo($date);
        }

    }

    /**
     * 新增用户统计表
     */
    public function userinfo($date){
        switch($date){
            case 'week':
                $data = "DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= from_unixtime(create_time,'%Y%m%d')";
                $fie = "from_unixtime(create_time,'%m-%d') ";
                break;
            case 'month':
                $data = "DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= from_unixtime(create_time,'%Y%m%d')";
                $fie = "from_unixtime(create_time,'%m-%d') ";
                break;
            case 'year':
                $data = "from_unixtime(create_time,'%Y')=YEAR(NOW())";
                $fie = "from_unixtime(create_time,'%Y-%m') ";
                break;
        }

        $week = User::where($data)->where('ord_agent',$this->agent_manage_id)->
        field($fie." week,count(id) sum")->group('week')->order("week","asc")->select()->toArray();
        $array['week'] = array_column($week,'week');
        $array['sum'] = array_column($week,'sum');
        return $array;
    }

    /**
     * 订单数据统计
     */
    public function order($date){
        switch($date){
            case 'week':
                $data = "DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= from_unixtime(ord_create_time,'%Y%m%d')";
                $fie = "from_unixtime(ord_create_time,'%m-%d') ";
                break;
            case 'month':
                $data = "DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= from_unixtime(ord_create_time,'%Y%m%d')";
                $fie = "from_unixtime(ord_create_time,'%m-%d') ";
                break;
            case 'year':
                $data = "from_unixtime(ord_create_time,'%Y')=YEAR(NOW())";
                $fie = "from_unixtime(ord_create_time,'%Y-%m') ";
                break;
        }

        $week = Orders::where('ord_status',1)->where($data)->where('ord_agent',$this->agent_manage_id)->
        field($fie." week,count(ord_id) sum")->group('week')->order("week","asc")->select()->toArray();
        $array['week'] = array_column($week,'week');
        $array['sum'] = array_column($week,'sum');
        return $array;
    }
}
